package gov.pbc.nn.kjgl.item.template.view;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.dao.CommonJdbcDao;
import gov.pbc.nn.kjgl.item.template.entity.ItemName;
import gov.pbc.nn.kjgl.item.template.entity.ItemType;

@Component
public class ItemTypePR {

	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hibernateDao;
	@Resource(name = CommonJdbcDao.BEAN_ID)
	private CommonJdbcDao jdbcDao;

	@DataProvider
	public void loadItemType(Page<ItemType> page, Criteria criteria) {
		DetachedCriteria dc = this.hibernateDao.buildDetachedCriteria(criteria, ItemType.class);
		dc.add(Restrictions.eq("invalid", false));
		this.hibernateDao.pagingQuery(page, dc);
	}

	@DataProvider
	public List<ItemName> loadItemName(String typeId) {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemName.class);
		dc.add(Restrictions.eq("itemTypeId", typeId));
		dc.add(Restrictions.eq("invalid", false));
		return (List<ItemName>) this.hibernateDao.query(dc);
	}

	@DataProvider
	public List<ItemType> getDeptAvaiableItemTypes(String typeName) {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemType.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.like("name", typeName));
		Dept dept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
		dc.add(Restrictions.like("deptId", dept.getCDept().getId()));
		return (List<ItemType>) this.hibernateDao.query(dc);
	}

	@DataResolver
	@Transactional
	public void saveItemType(ItemType it) {
		EntityState state = EntityUtils.getState(it);
		if (state == EntityState.NEW) {
			this.hibernateDao.save(it);
			List<ItemName> names = EntityUtils.getValue(it, "itemNames");
			if (names != null) {
				for (ItemName name : names) {
					name.setItemTypeId(it.getId());
					this.hibernateDao.save(name);
				}
			}
		} else if (state == EntityState.MODIFIED || state == EntityState.NONE) {
			List<ItemName> names = EntityUtils.getValue(it, "itemNames");
			if (state == EntityState.MODIFIED)
				this.hibernateDao.update(it);
			if (names != null) {
				for (ItemName name : names) {
					if (EntityUtils.getState(name) == EntityState.NEW) {
						name.setItemTypeId(it.getId());
						this.hibernateDao.save(name);
					}
					if (EntityUtils.getState(name) == EntityState.MODIFIED) {
						this.hibernateDao.update(name);
					}
				}
			}
		}
	}

	@Expose
	@Transactional
	public void deleteItemType(String id) {
		this.hibernateDao.delete(id, ItemType.class);
		List<ItemName> list = this.loadItemName(id);
		for (ItemName name : list) {
			this.hibernateDao.delete(name);
		}
	}

	@Expose
	@Transactional
	public void deleteItemName(String id) {
		this.hibernateDao.delete(id, ItemName.class);
	}

}
